package server;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.core.action.TimeAction;

import server.business.InformationManager;
import server.business.ranking.RankingManager;
import server.commonData.RoomManager;
import server.commonData.User;

/**
 * 游戏缓存
 * 游戏所有的非静态内存对象
 * @author King
 *
 */
public class GameCache extends TimeAction
{
	public GameCache() {
		super(1000);
		this.roomManager = new RoomManager();
		ServerEngin.getInstance().getExecutor().pushAction(this);
		ServerEngin.getInstance().getExecutor().pushAction(informationManager);
	}


	/**房间管理类**/
	private RoomManager roomManager;
	
	/**用户map**/
	private Map<Long, User> userMap = new HashMap<Long, User>();
	
	/**用户帐号map**/
	private Map<String, User> userAccountMap = new HashMap<String, User>();
	
	/**排行榜**/
	private RankingManager rankingManager = new RankingManager();
	
	/**信息广播**/
	private InformationManager informationManager = new InformationManager();
	
	
	
	/**
	 * 根据id获得用户
	 * @param userId
	 * @return
	 */
	public User getUser(long userId)
	{
		return this.userMap .get(userId);
	}
	
	
	public User getUser(String account)
	{
		return this.userAccountMap .get(account);
	}
	
	/**
	 * 添加一个用户
	 * @param id
	 * @param user
	 */
	public void addUser(User user)
	{
		userMap.put(user.getId(), user);
		userAccountMap.put(user.getAccount(), user);
	}
	/**
	 * 删除一用户
	 * @param userId
	 */
	public void delUser(long userId)
	{
		User user = this.userMap.remove(userId);
		if(user.getAccount()!=null)
			this.userAccountMap.remove(user.getAccount());
	}
	
	/**
	 * 房间管理器
	 * @return
	 */
	public RoomManager getRoomManager()
	{
		return roomManager;
	}


	public Map<Long, User> getUserMap() {
		return userMap;
	}


	public RankingManager getRankingManager() {
		return rankingManager;
	}


	@Override
	public void executeOnTime() throws Exception
	{
		long curMill = getCurTime();
		this.rankingManager.onTime(curMill);
		for(User user:userMap.values())
			user.onTime(curMill);
	}


	public InformationManager getInformationManager() {
		return informationManager;
	}
}
